package com.qinglei.recoup.system.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.common.service.RedisService;
import com.qinglei.recoup.common.utils.JsonUtils;
import com.qinglei.recoup.system.domain.HealthBigData;
import com.qinglei.recoup.system.domain.RealTimeMonitoring;
import com.qinglei.recoup.system.service.HealthBigDataService;
import com.qinglei.recoup.system.service.RealTimeMonitoringService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author dqh
 */
@RestController
@RequestMapping("/real-time-monitoring")
public class RealTimeMonitoringController {
    @Resource
    private RealTimeMonitoringService realTimeMonitoringService;

    @Resource
    private RedisService redisService;

    @PostMapping("/select")
    @Transactional(rollbackFor = Exception.class)
    public CommonRes selectRealTimeMonitoring(@RequestBody Map<String, Object> map) throws Exception {
        //重置默认值
        List<RealTimeMonitoring> list = realTimeMonitoringService.list(Wrappers.
                <RealTimeMonitoring>lambdaQuery()
                .eq(RealTimeMonitoring::getTenantId, map.get("tenantId")));
        return CommonRes.ok(list);
    }


    @PostMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public CommonRes updateRealTimeMonitoring(@RequestBody List<RealTimeMonitoring> realTimeMonitoringList) throws Exception {
        //修改                     updateBatchById
        realTimeMonitoringService.updateBatchById(realTimeMonitoringList);

        List<RealTimeMonitoring> RealTimeMonitoringList2 = realTimeMonitoringService.list(Wrappers.<RealTimeMonitoring>lambdaQuery().eq(RealTimeMonitoring::getTenantId, realTimeMonitoringList.get(0).getTenantId()));
        RealTimeMonitoringList2.forEach(f->{
            f.setCreateTime(null);
            f.setModifyTime(null);
        });
        String s1 = JsonUtils.objectToJson(RealTimeMonitoringList2);
        redisService.set("RealTimeMonitoring-"+RealTimeMonitoringList2.get(0).getTenantId(),s1);
        return CommonRes.ok("修改成功");
    }
    @PostMapping("/selectv2")
    @Transactional(rollbackFor = Exception.class)
    public CommonRes selectRealTimeMonitoringV2(@RequestBody Map<String, Object> map) throws Exception {
        //重置默认值
        //List<RealTimeMonitoring> list = realTimeMonitoringService.list(Wrappers.<RealTimeMonitoring>lambdaQuery().eq(RealTimeMonitoring::getTenantId, map.get("tenantId")));
        List<RealTimeMonitoring> realTimeMonitoring = realTimeMonitoringService.getRealTimeMonitoring("RealTimeMonitoring-"+map.get("tenantId"));
        return CommonRes.ok(realTimeMonitoring);
    }



}
